//
//  NewMessageViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/20.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewMessageViewController.h"
#import "JTCSM.h"
#import "MessageView.h"
#import "NewMessageJiangLiViewController.h"
static CGFloat changesize = 16;
static CGFloat changewidth = 8;
static CGFloat changeHeight = 5;
static CGFloat top = 30+64;
static CGFloat space = 20;
@interface NewMessageViewController ()
{
    CGFloat viewWidth ;
    CGFloat viewHeight;
}
@property (nonatomic,strong) NSMutableArray *dataSourceArray;
@property (nonatomic,strong) NSMutableArray *headerdataSourceArray;
@property (nonatomic,strong) NSMutableArray *HiddendataSourceArray;
@end

@implementation NewMessageViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)gotoJiangliVC{
    NewMessageJiangLiViewController *vc = [[NewMessageJiangLiViewController alloc]init];
    vc.nameTitle = @"奖励";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor =[UIColor whiteColor];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    self.navigationItem.rightBarButtonItem = [leftbtn setBarButtonOnlyItemsImageName:@"jiangliicon@3x.png" frame:CGRectMake(0,12,20,20) target:self SEL:@selector(gotoJiangliVC)];
    self.dataSourceArray = [NSMutableArray array];
    self.HiddendataSourceArray = [NSMutableArray array];
    self.headerdataSourceArray = [NSMutableArray array];
    [self layoutMessageViewWithDataSource:10 headerNumber:5];
    // Do any additional setup after loading the view.
}
- (void)layoutMessageViewWithDataSource:(NSInteger)dataArray headerNumber:(NSInteger)number{
    

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(space+changesize*2, top+changeHeight,KCONTROLLERWith-space*2-changesize*4 ,200)];
    view1.backgroundColor = RGBACOLOR(241, 241, 241, .8);
    [self.view addSubview:view1];
    view1.hidden = YES;
    [df layerWithView:view1 radius:5 width:1 color:RGBACOLOR(222, 222, 222, 1)];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(space+changesize+changewidth, top+changesize+changeHeight,KCONTROLLERWith-(space+changesize+changewidth)*2 ,200)];
    view2.backgroundColor = view1.backgroundColor;
    [self.view addSubview:view2];
    [df layerWithView:view2 radius:5 width:1 color:RGBACOLOR(222, 222, 222, 1)];
    view2.hidden = YES;
    [self.headerdataSourceArray addObject:view2];
    [self.headerdataSourceArray addObject:view1];
   
    
    viewWidth = KCONTROLLERWith-2*space;
    viewHeight = viewWidth*1.4106;
    for (int i = 0; i<dataArray; i++) {
        if (i ==1) {
            view2.hidden = NO;
        }
        if (i ==2) {
            view1.hidden = NO;
        }
        MessageView *view = [[MessageView alloc]initWithFrame:CGRectMake(space, top+changesize*2,KCONTROLLERWith-space*2 ,viewHeight)];
        [view setValueWithModel:[NSString stringWithFormat:@"%d",i] isShowNewImage:i==dataArray-1?YES:NO];
        [self.view addSubview:view];
        view.tag = 200+i;
        view.userInteractionEnabled = YES;
        UISwipeGestureRecognizer *rec=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
        rec.direction=UISwipeGestureRecognizerDirectionUp;
        [view addGestureRecognizer:rec];
        UISwipeGestureRecognizer *recdown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
        recdown.direction=UISwipeGestureRecognizerDirectionDown;
        [view addGestureRecognizer:recdown];
        [self.dataSourceArray addObject:view];
        if (i == dataArray-1) {
            view.hidden = NO;
        }else{
            view.hidden = YES;
        }
        

    }
    CGFloat buttonsize = 120;
    UIButton *bottomeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomeButton.frame = CGRectMake((KCONTROLLERWith-buttonsize)/2, KCONTROLLERHeight-buttonsize, buttonsize, buttonsize);
    [bottomeButton setImage:[UIImage imageNamed:@"按钮.png"] forState:UIControlStateNormal];
    [self.view addSubview:bottomeButton];
    [bottomeButton addTarget:self action:@selector(huidaodingbu) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)huidaodingbu{
    MessageView *showview =self.dataSourceArray.firstObject;
    showview.hidden = NO;
    for (NSInteger i = self.HiddendataSourceArray.count-1; i>=0; i--) {
        UIView *view = self.HiddendataSourceArray[i];
        
        [UIView animateWithDuration:0.3 animations:^{
            view.frame = CGRectMake(space, top+changesize*2, view.frame.size.width, view.frame.size.height);
            [self.dataSourceArray addObject:view];
            if (self.dataSourceArray.count == 3) {
                UIView *headerView = self.headerdataSourceArray.lastObject;
                headerView.hidden = NO;
            }
            if (self.dataSourceArray.count == 2) {
                UIView *headerView = self.headerdataSourceArray.firstObject;
                headerView.hidden = NO;
            }
        } completion:^(BOOL finished) {
            MessageView *mu = self.dataSourceArray.lastObject;
            mu.hidden = NO;
            for (MessageView *view in self.dataSourceArray) {
                if (mu != view) {
                    view.hidden = YES;
                }
            }
        }];
        

    }
    
    [self.HiddendataSourceArray removeAllObjects];
}
#pragma mark 上滑
- (void)swipeUp:(UISwipeGestureRecognizer *)direction{
    UIView *hiddenView = [direction view];
    if (self.HiddendataSourceArray.count == 0) {
        return;
    }
    UIView *view = self.HiddendataSourceArray.lastObject;
        [UIView animateWithDuration:0.3 animations:^{
           
            view.frame = CGRectMake(space, top+changesize*2, view.frame.size.width, view.frame.size.height);
            [self.dataSourceArray addObject:view];
            [self.HiddendataSourceArray removeLastObject];
            if (self.dataSourceArray.count == 3) {
                UIView *headerView = self.headerdataSourceArray.lastObject;
                headerView.hidden = NO;
            }
            if (self.dataSourceArray.count == 2) {
                UIView *headerView = self.headerdataSourceArray.firstObject;
                headerView.hidden = NO;
            }
        } completion:^(BOOL finished) {
            hiddenView.hidden = YES;
        }];
}
#pragma mark 下滑
- (void)swipeDown:(UISwipeGestureRecognizer *)direction{
    UIView *view = [direction view];
    if (self.dataSourceArray.count >1) {
        [UIView animateWithDuration:0.3 animations:^{
            UIView *showView = (UIView *)self.dataSourceArray[self.dataSourceArray.count-2];
            showView.hidden = NO;
            view.frame = CGRectMake(space, KCONTROLLERHeight, view.frame.size.width, view.frame.size.height);
            [self.HiddendataSourceArray addObject:view];
            [self.dataSourceArray removeLastObject];
            if (self.dataSourceArray.count == 2) {
                UIView *headerView = self.headerdataSourceArray.lastObject;
                headerView.hidden = YES;
            }
            if (self.dataSourceArray.count == 1) {
                UIView *headerView = self.headerdataSourceArray.firstObject;
                headerView.hidden = YES;
            }
        } completion:^(BOOL finished) {
            
        }];
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


//for (int i = 0; i<number; i++) {
//    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(space+changesize*(i+1), top+changeHeight,KCONTROLLERWith-(space+changesize*(i+1))*2 ,200)];
//    view1.backgroundColor = RGBACOLOR(241, 241, 241, .8);
//    [self.view addSubview:view1];
//    view1.hidden = YES;
//    [df layerWithView:view1 radius:5 width:1 color:RGBACOLOR(222, 222, 222, 1)];
//    [self.headerdataSourceArray addObject:view1];
//}
//NSInteger showHeaderviewnumber = self.HiddendataSourceArray.count-dataArray;
//for (int j = 0; j <((showHeaderviewnumber>=0)?showHeaderviewnumber:self.HiddendataSourceArray.count); j++) {
//    MessageView *headerview = self.HiddendataSourceArray[j];
//    headerview.hidden = NO;
//}

@end
